//
//  AppDelegate.m
//  Project_B
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "AppDelegate.h"
#import "APService.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    
    //新闻
    NewestViewController *newVC = [[NewestViewController alloc]init];
    UINavigationController *newNaVC = [[UINavigationController alloc]initWithRootViewController:newVC];
    
    
    
    //乐一乐
    LHSunnyViewController *jokeVC = [[LHSunnyViewController alloc]init];
    UINavigationController *jokeNaVC = [[UINavigationController alloc]initWithRootViewController:jokeVC];
    
    
    //视听
    VideoViewController *videoVC = [[VideoViewController alloc]init];
    UINavigationController *videoNaVC = [[UINavigationController alloc]initWithRootViewController:videoVC];
    
    //Mine
    MineViewController *mineVC = [[MineViewController alloc]init];
    UINavigationController *mineNaVC = [[UINavigationController alloc]initWithRootViewController:mineVC];
    
    
    //tabBar
    UITabBarController *tabBarC = [[UITabBarController alloc]init];
    
    
    
    //设置tabBar上面的内容
    newNaVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"新闻" image:[UIImage imageNamed:@"tabbar_icon_news_normal@2x.png"] selectedImage:[[UIImage imageNamed:@"tabbar_icon_news_highlight@2x.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    
    jokeNaVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"段子" image:[UIImage imageNamed:@"tabbar_icon_found_normal@2x.png"]selectedImage:[[UIImage imageNamed:@"tabbar_icon_found_highlight@2x.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    videoNaVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"视听" image:[UIImage imageNamed:@"tabbar_icon_media_normal@2x.png"] selectedImage:[[UIImage imageNamed:@"tabbar_icon_media_highlight@2x.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    mineNaVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"tabbar_icon_me_normal@2x.png"] selectedImage:[[UIImage imageNamed:@"tabbar_icon_me_highlight@2x.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    
    tabBarC.viewControllers = [NSArray arrayWithObjects:newNaVC,jokeNaVC,videoNaVC,mineNaVC, nil];
    
    
    self.window.rootViewController = tabBarC;
    
    
    
#pragma mark ** JPush
    // Required
    //判断    设备当前版本
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //categories
        [APService
         registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                             UIUserNotificationTypeSound |
                                             UIUserNotificationTypeAlert)
         categories:nil];
    } else {
        //categories nil
#pragma mark ** 第一步：注册远程通知
        //注册远程通知
        [APService
         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound |
                                             UIRemoteNotificationTypeAlert)
#else
         //categories nil
         categories:nil];
        [APService
         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                             UIRemoteNotificationTypeSound |
                                             UIRemoteNotificationTypeAlert)
#endif
         // Required
         categories:nil];
    }
    [APService setupWithOption:launchOptions];
    
    return YES;
}


#pragma mark ** 第二步：注册远程成功 返回Device Token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
#pragma mark ** 第三步：将返回的DevicToken 传给服务器（通常指所用的三方平台）
    // Required
    [APService registerDeviceToken:deviceToken];
}
#pragma mark ** 第四步：发送消息到APNS（服务器完成）
#pragma mark ** 第五步：收到远程通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required
    [APService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
